package es.deusto.eside.programacion4.mario2d.states;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import org.newdawn.slick.tiled.TiledMap;
import org.newdawn.slick.util.Log;

import es.deusto.eside.programacion4.mario2d.Mario2D;
import es.deusto.eside.programacion4.mario2d.model.FlowerAndGrow;
import es.deusto.eside.programacion4.mario2d.model.Mario;
import es.deusto.eside.programacion4.mario2d.utils.Camera;

/**
 * Segundo mapa del juego
 * 
 * @author Sergio
 * 
 */
public class SecondLevel extends StageModel {

	/**
	 * Constructor
	 * 
	 * @param ordinal
	 */
	public SecondLevel(int ordinal) {
		super(ordinal);
	}

	@Override
	public void init(GameContainer gC, StateBasedGame sb) {
		super.init(gC, sb);
	}

	@Override
	public void update(GameContainer gc, StateBasedGame sb, int delta) {
		super.update(gc, sb, delta);
		if (mario.getPositionX() > 1400) {
			for (int i = 0; i < enemies.size(); i++) {
				if (enemies.get(i).isDead() == true) {
					Mario2D.points += 100;
				}
			}
			for (int i = 0; i < bonus.size(); i++) {
				FlowerAndGrow f = bonus.get(i);
				if (f.isTaken() == true) {
					Mario2D.points += 100;
					Log.error("taken");
				}

			}
			Mario2D.points += (120000 - timer) / 100;
			sb.enterState(GameState.THIRD_LEVEL.ordinal(),
					new FadeOutTransition(), new FadeInTransition());
		}
		System.out.println("posicion x: " + mario.getPositionX());
		camera.centerOn(mario.getPositionX(), mario.getPositionY());
		world.step(timeStep, velocityIterations, positionIterations);
		mario.setPositionX(bPlayer.getPosition().x);
		mario.setPositionY(bPlayer.getPosition().y);
		System.out.println("posicion y: " + mario.getPositionY());
	}

	@Override
	public void initTile(GameContainer gC) throws SlickException {
		mario = new Mario();
		mario.setPositionX(50);
		mario.setPositionY(380);
		camera = new Camera(gC,
				new TiledMap("resources/Tile/mapaFonky/map.tmx"));
		solid = new int[24][75];
		for (int i = 0; i < 24; i++) {
			for (int j = 0; j < 75; j++) {
				// bloque
				if (camera.getMap().getTileId(j, i, 0) != 0) {
					solid[i][j] = 1;
				}
				// enemigo
				else if (camera.getMap().getTileId(j, i, 1) != 0) {
					solid[i][j] = 2;
				} else if (camera.getMap().getTileId(j, i, 2) != 0) {
					solid[i][j] = 3;
				} else {
					solid[i][j] = 0;
				}

			}
		}
		for (int i = 0; i < 24; i++) {
			for (int j = 0; j < 75; j++) {
				System.out.print(solid[i][j]);

			}
			System.out.println("");
		}
	}

	@Override
	public void initEnemies() {

	}

	@Override
	public void drawEnemies(Graphics g) {

	}

}
